df[]
df[condition]
Creates a new DataFrame containing only the rows that satisfy specific conditions.
- Input:
- condition : bool
- A sequence of boolean values with length equal to the number of rows of the DataFrame.
- Returns:
- new_df - A new DataFrame that contains only the rows whose corresponding element in condition is True.
- Return Type:
- DataFrame
pets
Index | ID | Species | Color | Weight | Age | Is_Cat | Owner_Comment |
---|---|---|---|---|---|---|---|
0 | dog_001 | dog | black | 40 | 5 | False | There are no bad dogs, only bad owners. |
1 | cat_001 | cat | golden | 1.5 | 0.2 | True | My best birthday present ever!!! |
2 | cat_002 | cat | black | 15 | 9 | True | ****All you need is love and a cat.**** |
3 | dog_002 | dog | white | 80 | 2 | False | Love is a wet nose and a wagging tail. |
4 | dog_003 | dog | black | 25 | 0.5 | False | Be the person your dog thinks you are. |
5 | ham_001 | hamster | black | 1 | 3 | False | No, thank you! |
6 | ham_002 | hamster | golden | 0.25 | 0.2 | False | No, thank you! |
7 | cat_003 | cat | black | 10 | 0 | True | No, thank you! |
Series of bools of if Species is a dog AND Color is white
(pets.get('Species')=='dog') & (pets.get('Color')=='white')
- 0False
- 1False
- 2False
- 3True
- 4False
- 5False
- 6False
- 7False
DataFrame where Species is a dog AND Color is white
pets[(pets.get('Species')=='dog') & (pets.get('Color')=='white')]
Index | ID | Species | Color | Weight | Age | Is_Cat | Owner_Comment |
---|---|---|---|---|---|---|---|
3 | dog_002 | dog | white | 80 | 2 | False | Love is a wet nose and a wagging tail. |
Series of bools where Species is a dog OR Color is white
(pets.get('Species')=='dog') | (pets.get('Color')=='white')
- 0True
- 1False
- 2False
- 3True
- 4True
- 5False
- 6False
- 7False
DataFrame where Species is a dog OR Color is white
pets[(pets.get('Species')=='dog') | (pets.get('Color')=='white')]
Index | ID | Species | Color | Weight | Age | Is_Cat | Owner_Comment |
---|---|---|---|---|---|---|---|
0 | dog_001 | dog | black | 40 | 5 | False | There are no bad dogs, only bad owners. |
3 | dog_002 | dog | white | 80 | 2 | False | Love is a wet nose and a wagging tail. |
4 | dog_003 | dog | black | 25 | 0.5 | False | Be the person your dog thinks you are. |
Some other examples!
pets[pets.get('Weight') >= 25]
Index | ID | Species | Color | Weight | Age | Is_Cat | Owner_Comment |
---|---|---|---|---|---|---|---|
0 | dog_001 | dog | black | 40 | 5 | False | There are no bad dogs, only bad owners. |
3 | dog_002 | dog | white | 80 | 2 | False | Love is a wet nose and a wagging tail. |
4 | dog_003 | dog | black | 25 | 0.5 | False | Be the person your dog thinks you are. |
pets[pets[(pets.get('Weight') >= 25) & (pets.get('Weight') < 80)]]
Index | ID | Species | Color | Weight | Age | Is_Cat | Owner_Comment |
---|---|---|---|---|---|---|---|
0 | dog_001 | dog | black | 40 | 5 | False | There are no bad dogs, only bad owners. |
4 | dog_003 | dog | black | 25 | 0.5 | False | Be the person your dog thinks you are. |
pets[pets[pets.get('Color').str.contains('e')]]
Index | ID | Species | Color | Weight | Age | Is_Cat | Owner_Comment |
---|---|---|---|---|---|---|---|
1 | cat_001 | cat | golden | 1.5 | 0.2 | True | My best birthday present ever!!! |
3 | dog_002 | dog | white | 80 | 2 | False | Love is a wet nose and a wagging tail. |
6 | ham_002 | hamster | golden | 0.25 | 0.2 | False | No, thank you! |
pets[pets1 = pets.set_index('ID')
pets1[pets1.index.str.contains('cat')]]
Index | Species | Color | Weight | Age | Is_Cat | Owner_Comment |
---|---|---|---|---|---|---|
cat_001 | cat | golden | 1.5 | 0.2 | True | My best birthday present ever!!! |
cat_002 | cat | black | 15 | 9 | True | ****All you need is love and a cat.**** |
cat_003 | cat | black | 10 | 0 | True | No, thank you! |
pets[pets.index > 3]
Index | ID | Species | Color | Weight | Age | Is_Cat | Owner_Comment |
---|---|---|---|---|---|---|---|
4 | dog_003 | dog | black | 25 | 0.5 | False | Be the person your dog thinks you are. |
5 | ham_001 | hamster | black | 1 | 3 | False | No, thank you! |
6 | ham_002 | hamster | golden | 0.25 | 0.2 | False | No, thank you! |
7 | cat_003 | cat | black | 10 | 0 | True | No, thank you! |